/*
 * AnchorValueDisplay.java
 *
 * Created on 11 January 2004, 11:07
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.task.anchor;

import com.velocityme.client.gui.calendar.CalendarDatePanel;
import com.velocityme.client.gui.responsibility.ResponsibilityValueDisplay;
import com.velocityme.interfaces.ContactablePK;
import com.velocityme.valueobjects.*;
import java.text.ParseException;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.swing.SpinnerNumberModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

/**
 *
 * @author  Robert Crida Work
 */
public class AnchorValueDisplay extends javax.swing.JPanel {
    
    private AnchorValue m_anchorValue;
    private ResponsibilityValueDisplay m_responsibilityValueDisplay = null;
    private ResponsibilityValueDisplay m_interestedValueDisplay = null;
    private CalendarDatePanel m_datePanel;
    
    /** Creates new form AnchorValueDisplay */
    public AnchorValueDisplay(AnchorValue p_anchorValue, ContactableValue p_defaultOwnerValue, Collection p_interestedContactableValues, Collection p_responsibleContactableValues, TreeModel p_allContactableValues, boolean p_displayResponsibility) {
        initComponents();
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 0);
        remove(jCheckBoxInterested);
        m_datePanel = new CalendarDatePanel();
        m_datePanel.setDate(new Date());
        add(m_datePanel, gridBagConstraints);
        if (p_displayResponsibility) {
            gridBagConstraints.weighty = 1.0;
            add(m_interestedValueDisplay = new ResponsibilityValueDisplay(), gridBagConstraints); 
            add(m_responsibilityValueDisplay = new ResponsibilityValueDisplay(), gridBagConstraints);
        }
        else {
            add(jCheckBoxInterested, gridBagConstraints);
            jLabelResponsibility.setVisible(false);
        }
        setAnchorValue(p_anchorValue, p_defaultOwnerValue, p_interestedContactableValues, p_responsibleContactableValues, p_allContactableValues, p_displayResponsibility);
    }
    
    public void setAnchorValue(AnchorValue p_anchorValue, ContactableValue p_defaultOwnerValue, Collection p_interestedContactableValues, Collection p_responsibleContactableValues, TreeModel p_allContactableValues, boolean p_displayResponsibility) {
        TaskValue taskValue = p_anchorValue.getTaskValue();
        NodeValue nodeValue = null;
        if (taskValue != null) {
            nodeValue = taskValue.getNodeValue();
            if (taskValue.priorityHasBeenSet())
                jSliderPriority.setValue(taskValue.getPriority().intValue());
            if (taskValue.getCreatorContactableValue() != null)
                jLabelCreatorName.setText(ContactableValueToString.toString(taskValue.getCreatorContactableValue()));
            m_datePanel.setDate(taskValue.getStartDate());
        }
        if (nodeValue != null && nodeValue.nameHasBeenSet())
            jTextFieldName.setText(nodeValue.getName());
        if (nodeValue != null && nodeValue.nodeIdHasBeenSet())
            jTextFieldId.setText(nodeValue.getNodeId().toString());
        if (nodeValue != null && nodeValue.descriptionHasBeenSet())
            jTextAreaDescription.setText(nodeValue.getDescription());
        jComboBoxOwner.removeAllItems();
        if (p_allContactableValues != null) {
            Object root = p_allContactableValues.getRoot();
            for (int i = 0; i < p_allContactableValues.getChildCount(root); i++) {
                Object nextNode = p_allContactableValues.getChild(root, i);
                recursiveAddContactable(p_allContactableValues, nextNode);
            }
            jComboBoxOwner.setSelectedItem(new ContactableValueToString(p_defaultOwnerValue));
        }
        else
            jComboBoxOwner.addItem(new ContactableValueToString(p_defaultOwnerValue));
        if (m_interestedValueDisplay != null)
            m_interestedValueDisplay.setAssignedValues(p_interestedContactableValues, p_allContactableValues);
        if (m_responsibilityValueDisplay != null)
            m_responsibilityValueDisplay.setAssignedValues(p_responsibleContactableValues, p_allContactableValues);
        m_anchorValue = p_anchorValue;
    }
    
    private void recursiveAddContactable(TreeModel p_allContactableValues, Object p_node) {
        jComboBoxOwner.addItem(((DefaultMutableTreeNode)p_node).getUserObject());
        for (int i = 0; i < p_allContactableValues.getChildCount(p_node); i++) {
            Object nextNode = p_allContactableValues.getChild(p_node, i);
            recursiveAddContactable(p_allContactableValues, nextNode);
        }
    }
    
    public AnchorValue getAnchorValue() {
        if (m_anchorValue.getTaskValue() == null) {
            m_anchorValue.setTaskValue(new TaskValue());
            m_anchorValue.getTaskValue().setNodeValue(new NodeValue());
        }
        try {
            m_anchorValue.getTaskValue().setStartDate(m_datePanel.getDate());
        }
        catch(ParseException e) {
            m_anchorValue.getTaskValue().setStartDate(new Date());
        }
        m_anchorValue.getTaskValue().setPriority(new Integer(jSliderPriority.getValue()));
        m_anchorValue.getTaskValue().setOwnerContactableValue((ContactableValue)jComboBoxOwner.getSelectedItem());
        m_anchorValue.getTaskValue().getNodeValue().setName(jTextFieldName.getText());
        m_anchorValue.getTaskValue().getNodeValue().setDescription(jTextAreaDescription.getText());
        return m_anchorValue;
    }
    
    public ContactablePK[] getResponsibleContactablePKs() {
        if (m_responsibilityValueDisplay != null)
            return m_responsibilityValueDisplay.getAssignedPKs();
        else
            return null;
    }
    
    public ContactablePK[] getInterestedContactablePKs() {
        if (m_interestedValueDisplay != null)
            return m_interestedValueDisplay.getAssignedPKs();
        else
            return null;
    }
    
    public boolean getInterestedDefault() {
        return jCheckBoxInterested.isSelected();
    }
    
    public void setEditable(boolean p_editable) {
        jTextFieldName.setEditable(p_editable);
        jLabelId.setVisible(!p_editable);
        jTextFieldId.setVisible(!p_editable);
        jTextAreaDescription.setEditable(p_editable);
        jSliderPriority.setEnabled(p_editable);
        jLabelCreator.setVisible(!p_editable);
        jLabelCreatorName.setVisible(!p_editable);
        jComboBoxOwner.setEnabled(p_editable);
        m_datePanel.setEnabled(p_editable);
        if (m_interestedValueDisplay != null)
            m_interestedValueDisplay.setEditable(p_editable);
        if (m_responsibilityValueDisplay != null)
            m_responsibilityValueDisplay.setEditable(p_editable);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jSpinner1 = new javax.swing.JSpinner();
        jLabelIcon = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jLabelId = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();
        jLabelDescription = new javax.swing.JLabel();
        jScrollPaneDescription = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jLabelPriority = new javax.swing.JLabel();
        jSliderPriority = new javax.swing.JSlider();
        jLabelCreator = new javax.swing.JLabel();
        jLabelCreatorName = new javax.swing.JLabel();
        jLabelOwner = new javax.swing.JLabel();
        jComboBoxOwner = new javax.swing.JComboBox();
        jLabelDate = new javax.swing.JLabel();
        jLabelInterested = new javax.swing.JLabel();
        jCheckBoxInterested = new javax.swing.JCheckBox();
        jLabelResponsibility = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jLabelIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/anchor-32x32.png")));
        add(jLabelIcon, new java.awt.GridBagConstraints());

        jLabelName.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelName, gridBagConstraints);

        jTextFieldName.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weightx = 1.0;
        add(jTextFieldName, gridBagConstraints);

        jLabelId.setText("ID#:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelId, gridBagConstraints);

        jTextFieldId.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jTextFieldId, gridBagConstraints);

        jLabelDescription.setText("Description:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelDescription, gridBagConstraints);

        jScrollPaneDescription.setPreferredSize(new java.awt.Dimension(106, 66));
        jTextAreaDescription.setEditable(false);
        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPaneDescription.setViewportView(jTextAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 1.0;
        add(jScrollPaneDescription, gridBagConstraints);

        jLabelPriority.setText("Priority:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelPriority, gridBagConstraints);

        jSliderPriority.setMajorTickSpacing(10);
        jSliderPriority.setPaintLabels(true);
        jSliderPriority.setPaintTicks(true);
        jSliderPriority.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jSliderPriority, gridBagConstraints);

        jLabelCreator.setText("Creator:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelCreator, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jLabelCreatorName, gridBagConstraints);

        jLabelOwner.setText("Owner:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelOwner, gridBagConstraints);

        jComboBoxOwner.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jComboBoxOwner, gridBagConstraints);

        jLabelDate.setText("Date:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelDate, gridBagConstraints);

        jLabelInterested.setText("Interested:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelInterested, gridBagConstraints);

        jCheckBoxInterested.setSelected(true);
        jCheckBoxInterested.setText("Add to interested contactables?");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jCheckBoxInterested, gridBagConstraints);

        jLabelResponsibility.setText("Responsibility:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelResponsibility, gridBagConstraints);

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBoxInterested;
    private javax.swing.JComboBox jComboBoxOwner;
    private javax.swing.JLabel jLabelCreator;
    private javax.swing.JLabel jLabelCreatorName;
    private javax.swing.JLabel jLabelDate;
    private javax.swing.JLabel jLabelDescription;
    private javax.swing.JLabel jLabelIcon;
    private javax.swing.JLabel jLabelId;
    private javax.swing.JLabel jLabelInterested;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelOwner;
    private javax.swing.JLabel jLabelPriority;
    private javax.swing.JLabel jLabelResponsibility;
    private javax.swing.JScrollPane jScrollPaneDescription;
    private javax.swing.JSlider jSliderPriority;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldName;
    // End of variables declaration//GEN-END:variables
    
}
